<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close @closed="$emit('closed')"
             align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="170" label-position="right">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item :label="$t('production.sendcode')" prop="deliver_code">
                        <el-input v-model="form.deliver_code" :placeholder="$t('all.auto')" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('sale.sale.sendtime')" prop="deliver_date">
                        <el-date-picker v-model="form.deliver_date" type="date" :placeholder="$t('all.select')" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="6">
                    <el-form-item :label="$t('procure.apply.name')" prop="supplier_id">
                        <el-select v-model="form.supplier_id" :placeholder="$t('all.select')" filterable>
                            <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col> -->
                <el-col :span="6">
                    <el-form-item :label="$t('production.backflush')" prop="is_reverse">
                        <el-switch v-model="form.is_reverse" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('production.materware')" prop="ware_id" v-if="form.is_reverse == 1">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" :placeholder="$t('goods.goods.ware')" clearable>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item :label="$t('all.attachment')" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">{{$t('all.selectattachment')}}</el-button>
                </sc-upload-file>
            </el-form-item>
            <el-col :span="12">
                <el-form-item :label="$t('all.remark')" prop="remark">
                    <el-input v-model="form.remark" :placeholder="$t('all.remark')" type="textarea"  clearable></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item :label="$t('procure.plan.product')" prop="detail">
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" :placeholder="$t('all.noData')" height="300" hideAdd>
                        <el-table-column prop="goods_code" :label="$t('goods.goods.code')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_code}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_name" :label="$t('goods.goods.name')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_name}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_model" :label="$t('goods.goods.model')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_model}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_spec" :label="$t('goods.goods.spec')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_spec}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit_name" :label="$t('goods.unit.unit')" align="center">
                            <template #default="scope">
                                {{scope.row.unit_name}}
                            </template>
                        </el-table-column>
                        <el-table-column :label="$t('production.productnum')" prop="plan_number" align="center">
                        </el-table-column>
                        <el-table-column :label="$t('production.send')" prop="can_number" align="center">
                        </el-table-column>
                        <el-table-column :label="$t('production.thissend')" prop="number" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" :placeholder="$t('production.thissend')" @focus="onFocus($event)" @input="sendInput(scope.row)"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">{{$t('all.cancel')}}</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">{{$t('all.save')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增生产单',
				send: this.$t('sale.sale.send'),
				house: '入库',
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                deliver_code: "",
                deliver_date: this.moment().format('YYYY-MM-DD'),
                enclosure: "",
                remark: "",
                detail: [],
                plan_id: "",
			},
			//验证规则
			rules: {
				contract_id: [{ required: true, message: '请选择销售' }],
			},
            disabled: false,
            applyList: [],
            wareList: [],
		}
	},
	mounted() {
        this.getApply();
        this.getWare()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.production.send.add.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success(this.$t('all.yes'))
					} else {
						this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.plan_id = data.id
            var res = await this.$API.production.plan.detail.get({id:data.id});
            let array = []
            res.data.detail.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    goods_code:item.goods && item.goods.goods_code,
                    goods_name:item.goods && item.goods.goods_name,
                    goods_spec:item.goods && item.goods.goods_spec,
                    goods_model:item.goods && item.goods.goods_model,
                    unit_name:item.goods && item.goods.unit.unit_name,
                    plan_number:item.number,
                    can_number:item.number - (item.deliver_number + item.enter_number),
                    number:item.number - (item.deliver_number + item.enter_number),
                })
            })
            this.form.detail = array
		},
        onFocus(event){
            event.target.select();
        },
        // 供应商列表
        async getApply(){
            var res = await this.$API.apply.apply.all.get();
            this.applyList = res.data
        },
        // 获取仓库
        async getWare(){
            var res = await this.$API.goods.ware.all.get();
            this.wareList = res.data;
        },
        sendInput(row){
            if(row.number > row.can_number){
                this.$message.error("发货数量不能大于可发货数量")
                row.number = row.can_number
            }
        },
	}
}
</script>

<style></style>
